package com.itheima.app.usermanager;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.*;
import com.itheima.service.BlackListService;
import com.itheima.service.NotificationService;
import com.itheima.service.QuestionService;
import com.itheima.service.UserInfoService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SettingManager {
    @DubboReference
    //注入陌生人问题接口
    private QuestionService questionService;
    @DubboReference
    //注入推送设置接口
    private NotificationService notificationService;
    //注入black接口
    @DubboReference
    private BlackListService blackListService;
    //注入userinfo接口
    @DubboReference
    private UserInfoService userInfoService;
      //查询通用设置需要的属性
    public ResponseEntity findSettingVo() {
        //从线程域中取出token
        User user = UserHolder.get();
        Long id = user.getId();
        //根据id查询陌生人问题
        Question question = questionService.findByUserId(id);
        //根据id查询推送设置
        Notification notification = notificationService.findByUserId(id);
        //创建设置实体类
        SettingVo settingVo = new SettingVo();
        settingVo.setId(id);
        settingVo.setPhone(user.getPhone());
        //判断黑名单问题是不是为空
        if (question!=null) {

            settingVo.setStrangerQuestion(question.getStrangerQuestion());
        }
        //判断通知设置是否为空
        if (notification!=null) {
            settingVo.setLikeNotification(notification.getLikeNotification());
            settingVo.setPinglunNotification(notification.getPinglunNotification());
            settingVo.setGonggaoNotification(notification.getGonggaoNotification());
        }
        return ResponseEntity.ok(settingVo);



    }
    //添加黑名单问题
    public void sava(String content) {
        //获得线程容器内user对象
        User user = UserHolder.get();
        //根据userid判断一些是否存在陌生人问题
        Question question = questionService.findByUserId(user.getId());
        //如同不存在
        if (question==null) {
         question.setUserId(user.getId());
         question.setStrangerQuestion(content);
         questionService.sava(question);
        }else {
            question.setUserId(user.getId());
            question.setStrangerQuestion(content);
            questionService.update(question);

        }


    }
//推送消息添加
    public void save(Notification param) {
        //根据id查询推送消息
        User user = UserHolder.get();
        Notification notification = notificationService.findByUserId(user.getId());
        //判断推动是否存在
        if (notification==null) {
            //不存在时
          param.setUserId(user.getId());
          notificationService.save(param);

        }else {
            notification.setLikeNotification(param.getLikeNotification());
            notification.setGonggaoNotification(param.getGonggaoNotification());
            notification.setPinglunNotification(param.getPinglunNotification());
            notificationService.update(notification);
        }

    }

    public ResponseEntity blackfindByPage(Integer pageNum, Integer pageSize) {
        Long id = UserHolder.get().getId();
        //获得黑名单对应userid的数据
        PageBeanVo page = blackListService.findByPage(id, pageNum, pageSize);
        //遍历实体类中的集合，泛型是blacklist
        List<BlackList> blackListList = (List<BlackList>) page.getItems();
        //创建一个userinfo的实体类集合，用来封装最后的结果
        ArrayList<UserInfo> userInfoList = new ArrayList<>();
        if (blackListList!=null&& blackListList.size() > 0){
        for (BlackList blackList : blackListList) {
            Long blackUserId = blackList.getBlackUserId();
            UserInfo us = userInfoService.select(blackUserId);
            userInfoList.add(us);
        }
        }
        page.setItems(userInfoList);
        return ResponseEntity.ok(page);

    }

    public void delete(Integer uid) {
        Long id = UserHolder.get().getId();
        blackListService.delete(uid,id);
    }
}
